package cqut.wys;

import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBFactory;
import org.iq80.leveldb.Options;
import org.iq80.leveldb.impl.Iq80DBFactory;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * --------神兽出没--------/
 *   ┌─┐     ┌─┐
 * ┌─┘─┴─────┘─┴─┐
 * │      ─      │
 * │  ┬─┘   └─┬  │
 * │             │
 * │      ┴      │  Code is far away from bug with the animal protecting
 * └───┐      ┌──┘  神兽保佑,代码无bug
 *     │      │
 *     │      └──────┐
 *     │             ├┐
 *     │             ┌┘
 *     └┐ ┐ ┌───┬─┐ ┌┘
 *      │ ┤ ┤   │ ┤ ┤
 *      └─┴─┘   └─┴─┘
 * -----------------------/
 *
 * @author wys
 * @date 2019/11/7
 */
public class LevelDBDemo {

    private static final String LEVEL_DB_PATH = System.getProperty("user.dir") + "\\data\\leveldb";
    private static final Charset CHARSET = Charset.forName("utf-8");

    public static void main(String[] args) {
        DBFactory factory = new Iq80DBFactory();
        // 默认如果没有则创建
        Options options = new Options();
        File file = new File(LEVEL_DB_PATH);
        DB db = null;
        try {
            db = factory.open(file, options);
            byte[] keyByte1 = "key-01".getBytes(CHARSET);
            byte[] keyByte2 = "key-02".getBytes(CHARSET);
            // 会写入磁盘中
            db.put(keyByte1, "value-01".getBytes(CHARSET));
            db.put(keyByte2, "value-02".getBytes(CHARSET));
            String value1 = new String(db.get(keyByte1), CHARSET);
            System.out.println(value1);
            System.out.println(new String(db.get(keyByte2), CHARSET));
            long startTime1 = System.currentTimeMillis();
            batchPut(db);
            long startTime2 = System.currentTimeMillis();
            System.out.println(startTime2 -startTime1);
            batchGet(db);
            long startTime3 = System.currentTimeMillis();
            System.out.println(startTime3 -startTime2);
            batchDelete(db);
            long startTime4 = System.currentTimeMillis();
            System.out.println(startTime4 -startTime3);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                try {
                    db.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void batchPut(DB db){
        for (int i = 0; i < 1000000; i++) {
            byte[] key = new String("key" + i).getBytes();
            byte[] value = new String("value" + i).getBytes();
            db.put(key, value);
        }
    }

    private static void batchGet(DB db){
        for (int i = 0; i < 1000000; i++) {
            byte[] key = new String("key" + i).getBytes();
            byte[] value = db.get(key);
            String targetValue = "value" + i;
            if (!new String(value).equals(targetValue)) {
                System.out.println("something wrong!");
            }
        }
    }

    private static void batchDelete(DB db){
        for (int i = 0; i < 1000000; i++) {
            byte[] key = new String("key" + i).getBytes();
            db.delete(key);
        }
    }

}
